home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1320 / 1320.xpi / components / gmCookies.js < prev    next >
Text File  |  2010-01-22  |  8KB  |  237 lines

  1. // Gmail Manager
  2. // By Todd Long <longfocus@gmail.com>
  3. // http://www.longfocus.com/firefox/gmanager/
  4.  
  5. function gmCookies()
  6. {
  7.   // Load the services
  8.   this._cookieManager = Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager);
  9.   this._cookieService = Components.classes["@mozilla.org/cookieService;1"].getService(Components.interfaces.nsICookieService);
  10.   this._observer = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  11.   
  12.   // Initialize the sessions
  13.   this._sessions = new Array();
  14. }
  15. gmCookies.prototype = {
  16.   loadSession: function(aHost)
  17.   {
  18.     // Check for host session
  19.     if (this._sessions[aHost] != null)
  20.     {
  21.       // Increment connections
  22.       this._sessions[aHost].count++;
  23.     }
  24.     else
  25.     {
  26.       // Creates host session
  27.       this._sessions[aHost] = new Object();
  28.       this._sessions[aHost].count = 1;
  29.       this._sessions[aHost].cookies = this._getCookies(aHost);
  30.       
  31.       // Load observers
  32.       this._observer.addObserver(this, "cookie-changed", false);
  33.       this._observer.addObserver(this, "http-on-modify-request", false);
  34.       this._observer.addObserver(this, "http-on-examine-response", false);
  35.     }
  36.   },
  37.   
  38.   restoreSession: function(aHost)
  39.   {
  40.     // Check for host session
  41.     if (this._sessions[aHost] != null)
  42.     {
  43.       // Decrement connections
  44.       this._sessions[aHost].count--;
  45.       
  46.       if (this._sessions[aHost].count == 0)
  47.       {
  48.         // Remove observers
  49.         this._observer.removeObserver(this, "cookie-changed");
  50.         this._observer.removeObserver(this, "http-on-modify-request");
  51.         this._observer.removeObserver(this, "http-on-examine-response");
  52.         
  53.         // Restore cookies
  54.         this._putCookies(aHost, this._sessions[aHost].cookies);
  55.         
  56.         // Remove host session
  57.         delete this._sessions[aHost];
  58.       }
  59.     }
  60.   },
  61.   
  62.   _getCookies: function(aHost)
  63.   {
  64.     var iioServ = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  65.     var cookieEnum = this._cookieManager.enumerator;
  66.     var cookies = new Array();
  67.     
  68.     while (cookieEnum.hasMoreElements())
  69.     {
  70.       var nextCookie = cookieEnum.getNext().QueryInterface(Components.interfaces.nsICookie2);
  71.       
  72.       if (nextCookie.host.indexOf(aHost) > -1)
  73.       {
  74.         var pair = nextCookie.name + "=" + nextCookie.value + ";";
  75.         var type = (nextCookie.isDomain ? "Domain=" : "Host=") + nextCookie.host + ";";
  76.         var path = "Path=" + nextCookie.path + ";";
  77.         var expires = (nextCookie.isSession ? "" : "Expires=" + (new Date(nextCookie.expires * 1000).toGMTString()));
  78.         
  79.         // Creates cookie
  80.         cookies[nextCookie.name] = new Object();
  81.         cookies[nextCookie.name].set = pair + type + path + expires;
  82.         cookies[nextCookie.name].uri = iioServ.newURI("http://" + nextCookie.rawHost, "UTF-8", null);
  83.         cookies[nextCookie.name].value = nextCookie.value;
  84.       }
  85.     }
  86.     
  87.     return cookies;
  88.   },
  89.   
  90.   _putCookies: function(aHost, aCookies)
  91.   {
  92.     var cookieEnum = this._cookieManager.enumerator;
  93.     
  94.     // Clear host cookies
  95.     while (cookieEnum.hasMoreElements())
  96.     {
  97.       var cookie = cookieEnum.getNext().QueryInterface(Components.interfaces.nsICookie2);
  98.       var remove = (cookie.name in aCookies ? cookie.value != aCookies[cookie.name].value : true);
  99.       
  100.       if (cookie.host.indexOf(aHost) > -1 && remove)
  101.         this._cookieManager.remove(cookie.host, cookie.name, cookie.path, false);
  102.     }
  103.     
  104.     // Restore host cookies
  105.     for (var name in aCookies)
  106.       this._cookieService.setCookieString(aCookies[name].uri, null, aCookies[name].set, null);
  107.   },
  108.   
  109.   _inSessions: function(aHost)
  110.   {
  111.     var host = null;
  112.     
  113.     for (var lookupHost in this._sessions)
  114.       if (aHost.indexOf(lookupHost) > -1)
  115.         host = lookupHost;
  116.     
  117.     return host;
  118.   },
  119.   
  120.   observe: function(aSubject, aTopic, aData)
  121.   {
  122.     switch (aTopic)
  123.     {
  124.       case "cookie-changed":
  125.       {
  126.         var inCookie = aSubject.QueryInterface(Components.interfaces.nsICookie2);
  127.         var host = this._inSessions(inCookie.host);
  128.         
  129.         if (host != null)
  130.         {
  131.           var myCookie = this._sessions[host].cookies[inCookie.name];
  132.           
  133.           if (myCookie && myCookie.value != inCookie.value)
  134.             this._cookieService.setCookieString(myCookie.uri, null, myCookie.set, null);
  135.         }
  136.         
  137.         break;
  138.       }
  139.       case "http-on-modify-request":
  140.       case "http-on-examine-response":
  141.       {
  142.         var httpChannel = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
  143.         var uri = httpChannel.URI;
  144.         var host = this._inSessions(uri.host);
  145.         var isGood = (uri.originCharset.indexOf("gmanager") == -1 && host != null);
  146.         
  147.         if (aTopic == "http-on-modify-request" && isGood)
  148.         {
  149.           try {
  150.             var session = this._sessions[host];
  151.             
  152.             // Clear any cookies
  153.             httpChannel.setRequestHeader("Cookie", "", false);
  154.             
  155.             for (var name in session.cookies)
  156.             {
  157.               var cookieValue = (session.cookies[name].set.split(";"))[0];
  158.               var cookieHost = session.cookies[name].uri.host;
  159.               
  160.               if (uri.host.indexOf(cookieHost) > -1)
  161.                 httpChannel.setRequestHeader("Cookie", cookieValue, true);
  162.             }
  163.           } catch(e) {}
  164.         }
  165.         else if (aTopic == "http-on-examine-response" && isGood)
  166.         {
  167.           try {
  168.             var cookies = httpChannel.getResponseHeader("Set-Cookie").split("\n");
  169.             
  170.             for (var i = 0; i < cookies.length; i++)
  171.             {
  172.               var pair = (cookies[i].split(";"))[0];
  173.               var name = (pair.split("="))[0];
  174.               
  175.               this._sessions[host].cookies[name] = new Object();
  176.               this._sessions[host].cookies[name].set = cookies[i];
  177.               this._sessions[host].cookies[name].uri = uri;
  178.               this._sessions[host].cookies[name].value = (pair.split("="))[1];
  179.             }
  180.           } catch(e) {}
  181.         }
  182.         
  183.         break;
  184.       }
  185.     }
  186.   },
  187.   
  188.   QueryInterface: function(iid)
  189.   {
  190.     if (iid.equals(Components.interfaces.gmICookies) ||
  191.         iid.equals(Components.interfaces.nsISupports))
  192.       return this;
  193.     throw Components.results.NS_ERROR_NO_INTERFACE;
  194.   }
  195. }
  196.  
  197. var myModule = {
  198.   firstTime: true,
  199.   
  200.   myCID: Components.ID("{81516840-f7dd-11da-974d-0800200c9a66}"),
  201.   myDesc: "Cookie Sessions Manager",
  202.   myProgID: "@longfocus.com/gmanager/cookies;1",
  203.   myFactory: {
  204.     createInstance: function (outer, iid) {
  205.       if (outer != null)
  206.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  207.       
  208.       return (new gmCookies()).QueryInterface(iid);
  209.     }
  210.   },
  211.   
  212.   registerSelf: function (compMgr, fileSpec, location, type)
  213.   {
  214.     if (this.firstTime) {
  215.       this.firstTime = false;
  216.       throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  217.     }
  218.     
  219.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  220.     compMgr.registerFactoryLocation(this.myCID, this.myDesc, this.myProgID, fileSpec, location, type);
  221.   },
  222.   
  223.   getClassObject: function (compMgr, cid, iid)
  224.   {
  225.     if (!cid.equals(this.myCID))
  226.       throw Components.results.NS_ERROR_NO_INTERFACE;
  227.     
  228.     if (!iid.equals(Components.interfaces.nsIFactory))
  229.       throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  230.     
  231.     return this.myFactory;
  232.   },
  233.   
  234.   canUnload: function(compMgr) { return true; }
  235. };
  236.  
  237. function NSGetModule(compMgr, fileSpec) { return myModule; }